//
//  Data.h
//  chatv
//
//  Created by 云 浮 on 12-7-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#ifndef _Data_h
#define _Data_h

#include "NetAPI.h"

struct sSessionInfo
{
    int             fd ;        // socket id.
    unsigned int    uuid ;      // user id.
    int             index ;     // user id. server set. single session.
    int             lobbyID ;   // connect lobby id.
    char            ip[32] ;    // client ip.
    int             port ;      // client port.
} ;

class CData
{
public:
    static CData* Instance() ;
    void initData() ;

private:
    CData() ;
    ~CData() ;

private:
    static CData* pData ;

// about session functions.
public:
    // add a client session, return add index.
    int addSession(const int fd, const int uuid, const int lobbyID, const char* ip, const int port) ;

    // add a client session, return add index.
    int addSession(const sSessionInfo info) ;

    // delete all.
    int clearSessions() ;
    
    // delete by fd, return delete index or -1.
    int delSessionByFD(const int fd) ;
    
    // delete by uuid, return delete index or -1.
    int delSessionByUUID(const int uuid) ;
    
    // delete by index, return delete index or -1.
    int delSessionByIndex(const int index) ;
    
    // delete by lobbyID, return delete count.
    int delSessionByLobbyID(const int lobbyID) ;
    
    // delete by ip, return delete count.
    int delSessionByIP(const char* ip) ;
    
    // delete by port, return delete count.
    int delSessionByPort(const int port) ;

    const sSessionInfo* getSessionInfoByFD(const int fd) const ;
    const sSessionInfo* getSessionInfoByUUID(const int uuid) const ;
    const sSessionInfo* getSessionInfoByIndex(const int index) const ;

// about socket functions.
public:
    int sendToLogin(const void* data, const int len) ;
    int sendToLobby(const void* data, const int len, const int lobbyID) ;
    int sendToClient(const void* data, const int len, const int fd) ;
    int sendToIP(const void* data, const int len, const char* ip, const int port) ;

private:
    sSessionInfo* sessionInfos ;

    DECLARE_VARIABLE_READYONLY(loginInfo,       LoginInfo,      FreeNet::sNetInfo)
    DECLARE_VARIABLE_READYONLY(lobbyInfos,      LobbyInfos,     FreeNet::sNetInfo*)
    DECLARE_VARIABLE_READYONLY(localInfo,       LocalInfo,      FreeNet::sNetInfo)
    DECLARE_VARIABLE_READYONLY(lobbyMax,        LobbyMax,       int)
    DECLARE_VARIABLE_READYONLY(sessionMax,      SessionMax,     int)
    DECLARE_VARIABLE_READYONLY(sessionCount,    SessionCount,   int)

    DECLARE_VARIABLE_READYONLY(loginThread,     LoginThread,    pthread_t)
    DECLARE_VARIABLE_READYONLY(lobbyThread,     LobbyThread,    pthread_t)
    DECLARE_VARIABLE_READYONLY(sessionThread,   SessionThread,  pthread_t)
    FreeNet::CEpoll* pEpoll ;

    DECLARE_VARIABLE_READYONLY(loginFlag,       LoginFlag,      bool)
    DECLARE_VARIABLE_READYONLY(lobbyFlag,       LobbyFlag,      bool)
    DECLARE_VARIABLE_READYONLY(sessionFlag,     SessionFlag,    bool)

    DECLARE_VARIABLE_READYONLY(loginMsgID,      LoginMsgID,     int)
    DECLARE_VARIABLE_READYONLY(lobbyMsgID,      LobbyMsgID,     int)
    DECLARE_VARIABLE_READYONLY(sessionMsgID,    SessionMsgID,   int)
} ;

extern void* login_recv(void* param) ;
extern void* lobby_recv(void* param) ;
extern void* session_recv(void* param) ;

#endif














